package mock.servicesdirectory.contacts.dao.daoimpl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import mock.servicesdirectory.common.entities.Contact;
import mock.servicesdirectory.contacts.dao.daointerface.ContactDao;

public class ContactDaoImpl extends HibernateDaoSupport implements ContactDao {

	/**
	 * Method used to get all contact from database
	 */
	@Override
	public List<Contact> getContacts() {
		String sql = "from Contact";

		@SuppressWarnings("unchecked")
		List<Contact> contacts = getHibernateTemplate().find(sql);

		return contacts;
	}

	/**
	 * Method used to get all active contact fromd database
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Contact> getContacts(boolean isActive) {
		/*
		 * Use ? to pass paramter to sql string
		 */
		/*
		 * String sql = "from Contact as contact where contact.isActive=?";
		 * 
		 * @SuppressWarnings("unchecked") List<Contact> contacts =
		 * getHibernateTemplate().find(sql,isActive); return contacts;
		 */
		String sql;
		List<Contact> contacts;
		if (isActive) {
			sql = "from Contact as contact where contact.isActive=:isActive";
			Session session = this.getSession();
			Query query = session.createQuery(sql);
			query.setBoolean("isActive", isActive);
			contacts = query.list();

		} else {
			sql = "from Contact";
			contacts = getHibernateTemplate().find(sql);
		}
		return contacts;
	}

	/**
	 * Method used to get all contact with filter by first name, sur name
	 */
	@Override
	public List<Contact> getContacts(String firstName, String surName) {
		String sql = "from Contact as contact "
				+ "where contact.firstName like :firstName "
				+ "and contact.surName like :surName";

		Session session = this.getSession();
		Query query = session.createQuery(sql);
		query.setString("firstName", firstName + "%");
		query.setString("surName", surName + "%");

		@SuppressWarnings("unchecked")
		List<Contact> contacts = query.list();

		return contacts;
	}

	/**
	 * Method used to get all contact with filter by first name, sur name,
	 * isActive
	 */
	@Override
	public List<Contact> getContacts(String firstName, String surName,
			boolean isActive) {
		String sql = "from Contact as contact "
				+ "where contact.firstName like :firstName "
				+ "and contact.surName like :surName "
				+ "and contact.isActive=:isActive";

		Session session = this.getSession();
		Query query = session.createQuery(sql);
		query.setString("firstName", firstName + "%");
		query.setString("surName", surName + "%");
		query.setBoolean("isActive", isActive);

		@SuppressWarnings("unchecked")
		List<Contact> contacts = query.list();

		return contacts;
	}

	/**
	 * Method used to get all contact with filter by first name
	 */
	@Override
	public List<Contact> getContactsFilterFirstName(String firstName) {
		String sql = "from Contact as contact "
				+ "where contact.firstName like :firstName ";

		Session session = this.getSession();
		Query query = session.createQuery(sql);
		query.setString("firstName", firstName + "%");

		@SuppressWarnings("unchecked")
		List<Contact> contacts = query.list();

		return contacts;
	}

	/**
	 * Method used to get all contact with filter by first name, isactive
	 */
	@Override
	public List<Contact> getContactsFilterFirstName(String firstName,
			boolean isActive) {
		String sql = "from Contact as contact "
				+ "where contact.firstName like :firstName "
				+ "and contact.isActive=:isActive";

		Session session = this.getSession();
		Query query = session.createQuery(sql);
		query.setString("firstName", firstName + "%");
		query.setBoolean("isActive", isActive);

		@SuppressWarnings("unchecked")
		List<Contact> contacts = query.list();

		return contacts;
	}

	/**
	 * Method used to get all contact with filter by surname
	 */
	@Override
	public List<Contact> getContactsFilterSurName(String surName) {
		String sql = "from Contact as contact "
				+ "where contact.surName like :surName ";

		Session session = this.getSession();
		Query query = session.createQuery(sql);
		query.setString("surName", surName + "%");

		@SuppressWarnings("unchecked")
		List<Contact> contacts = query.list();

		return contacts;
	}

	/**
	 * Method used to get all contact with filter by surname, isactive
	 */
	@Override
	public List<Contact> getContactsFilterSurName(String surName,
			boolean isActive) {
		String sql = "from Contact as contact "
				+ "where contact.surName like :surName "
				+ "and contact.isActive=:isActive";

		Session session = this.getSession();
		Query query = session.createQuery(sql);
		query.setString("surName", surName + "%");
		query.setBoolean("isActive", isActive);

		@SuppressWarnings("unchecked")
		List<Contact> contacts = query.list();

		return contacts;
	}

	/**
	 * Method used to get contact corresponse whith contactID from database
	 * 
	 * @param contactID
	 * @return
	 */
	@Override
	public Contact getContact(int contactID) {
		String sql = "from Contact as c where c.contactId=:contactID";
		Session session = this.getSession();
		Query query = session.createQuery(sql);
		query.setInteger("contactID", contactID);

		@SuppressWarnings("unchecked")
		List<Contact> contacts = query.list();
		if (contacts == null || contacts.size() == 0) {
			return null;
		} else {
			return contacts.get(0);
		}
	}

	/**
	 * Method used to insert or update contact into database
	 */
	@Override
	public boolean saveOrUpdateContact(Contact contact) {
		// this.getSessionFactory().getCurrentSession().saveOrUpdate(contact);
		getHibernateTemplate().saveOrUpdate(contact);
		return true;
	}
}
